﻿using UnityEngine;
using UnityEngine.UI;

namespace HKTools
{
    /// <summary>
    /// 自定义的UI组件，用于在UGUI中绘制2D曲线
    /// </summary>
    public class HK_Curve2D : Image
    {
        [Header("曲线："), SerializeField] AnimationCurve curve; // 曲线形状
        [Header("曲线宽度："), SerializeField] float lineWidth = 4; // 曲线的宽度
        [Header("精度："), SerializeField, Range(0.1f, 1f)] float accuracy = 0.2f;  // 曲线的绘制精度

        [Header("纹理平铺："), SerializeField] float textureTiling = 0.5f; // 控制纹理的平铺
        [Header("纹理偏移："), SerializeField] float textureOffset = 0.0f; // 控制纹理的偏移，实现蚂蚁线的动画效果

        // 重写MaskableGraphic的OnPopulateMesh方法，用于生成曲线的网格
        protected override void OnPopulateMesh(VertexHelper vh)
        {
            var rect = this.rectTransform.rect; // 获取UI组件的矩形区域
            var pivotY = rectTransform.pivot.y; // 获取UI组件的Y轴锚点
            float step = 10 / accuracy; // 根据精度计算步长

            vh.Clear();  // 清除所有顶点数据，准备重新绘制

            // 计算起始点位置
            Vector2 lastPos = new Vector2(rect.xMin, CalcY(0, pivotY) * rect.height);

            // 循环生成曲线上的点，并用四边形连接
            for (float x = rect.xMin + step; x < rect.xMax; x += step)
            {
                Vector2 pos = new Vector2(x, CalcY((x - rect.xMin) / rect.width, pivotY) * rect.height);
                AddQuad(vh, lastPos, pos);
                lastPos = pos;
            }

            // 计算终点位置，并添加最后一个四边形
            //Vector2 endPos = new Vector2(rect.xMax, CalcY(1, pivotY) * rect.height);
            //AddQuad(vh, lastPos, endPos);

            // 添加三角形，用于渲染四边形
            for (int i = 0; i < vh.currentVertCount - 4; i += 4)
            {
                vh.AddTriangle(i + 1, i + 2, i + 4);
                vh.AddTriangle(i + 1, i + 2, i + 7);
            }
        }

        // 根据曲线计算Y坐标
        float CalcY(float time, float pivotY)
        {
            return curve.Evaluate(time) - pivotY;
        }

        // 添加一个四边形到VertexHelper
        void AddQuad(VertexHelper vh, Vector2 pos1, Vector2 pos2)
        {
            // 计算两点间距离
            float dis = Vector2.Distance(pos1, pos2);
            // 计算Y方向上的偏移
            float y = lineWidth * (pos2.x - pos1.x) / dis;
            // 计算X方向上的偏移
            float x = lineWidth * (pos2.y - pos1.y) / dis;

            // 修正偏移方向，确保线宽是正确的
            if (y <= 0)
                y = -y;
            else
                x = -x;

            // 创建四个顶点
            UIVertex[] vertex = new UIVertex[4];

            // 设置四个顶点的位置
            vertex[0].position = new Vector2(pos1.x + x, pos1.y + y);
            vertex[1].position = new Vector2(pos2.x + x, pos2.y + y);
            vertex[2].position = new Vector2(pos2.x - x, pos2.y - y);
            vertex[3].position = new Vector2(pos1.x - x, pos1.y - y);

            vertex[0].uv0 = new Vector2(1, 1 * textureTiling + textureOffset);
            vertex[1].uv0 = new Vector2(1, textureOffset);
            vertex[2].uv0 = new Vector2(0, textureOffset);
            vertex[3].uv0 = new Vector2(0, 1 * textureTiling + textureOffset);

            // 设置顶点的颜色
            for (int i = 0; i < vertex.Length; i++)
            {
                vertex[i].color = color;
            }

            // 将四边形的顶点添加到VertexHelper
            vh.AddUIVertexQuad(vertex);
        }
    }
}